<?php
namespace app\common\model;


use app\extra\BaseModel;
use app\extra\DBCont;

class Match extends BaseModel
{
    protected $auto = ['user_id'];
    public function setUserIdAttr()
    {
        return getUserID();
    }
    public function setAuthAttr($value){
        if (!empty($value)){
            return $value;
        }
        $name = getUserName();
        return  empty($name)? '匿名作者' : $name;
    }
//    // 开始时间
//    public function setStartTimeAttr($value){
//        return strtotime($value);
//    }
//    public function getStartTimeAttr($value){
//        return date('Y-m-d H:i',$value);
//    }
//    // 结束时间
//    public function setEndTimeAttr($value){
//        return strtotime($value);
//    }
//    public function getEndTimeAttr($value){
//        return date('Y-m-d H:i',$value);
//    }
    // 分类
    public function setCateIdAttr($value){
        return serializeByArray($value, ',');
    }
    public function getCateIdAttr($value){
        $data = unSerializeByJson($value, ',');
        $arr = [];
        foreach ($data as $v){
            $arr[] = [
                'key' => $v,
                'value' => model('MatchCate')->where('id', $v)->value('name')
            ];
        }
        return $arr;
    }
    public function getIsFeesAttr($value){
        return [
                'key' => $value,
                'value' => DBCont::getFeesType($value)
            ];
    }
    // 图片
    public function setImagesAttr($value){
        if (is_array($value)){
            $arr = [];
            foreach ($value as $item){
                if (!empty($item)){
                    $arr[] = $item;
                }
            }
            return serializeByArray($arr, ',');
        }else {
            return $value;
        }

    }
    public function getImagesAttr($value){
        $data = unSerializeByJson($value, ',');
        $arr = [];
        foreach ($data as $v){
            $arr[] = [
                'key' => $v,
                'value' => showFileUrl($v)
            ];
        }
        return $arr;
    }
    /**
     * 添加额外参数
     * @param $item
     */
    public function setAddedField(&$item){
//        $item['title'] = model('Goods')->getAttr();
        $item['user_desc'] = model('Admin')->getUserName($item['user_id']);
        $item['status_desc'] = DBCont::getMatchStatus($item['status']);
        if ($item['status'] == DBCont::SUCCESS){
            if($item['start_time'] > time()){
                $item['status_desc'] = DBCont::getMatchStatus(DBCont::WAIT);
            }elseif($item['end_time'] < time()){
                $item['status_desc'] = DBCont::getMatchStatus(DBCont::CANCEL);
            }
        }

        $team_id = model('Team')->where('match_id', $item['id'])->column('id');
        if (empty($team_id)){
            $item['people_number'] = 0;
            $item['team_count'] = 0;
            $item['pay_count'] = 0;
            $item['member_count'] = 0;
        } else {
            $item['people_number'] = model('Member')->whereIn('team_id', $team_id)
//                ->where('pay_state', DBCont::TEAM)
//                ->where('status', DBCont::SUCCESS)
                ->count();
            $item['pay_count'] = model('Member')
                ->whereIn('team_id', $team_id)
                ->where('pay_state', DBCont::SUCCESS)
//                ->where('bill', '<>', '')
                ->count();
            $team = model('Member')->whereIn('team_id', $team_id)
                ->where('pay_state', DBCont::SUCCESS)
//                ->where('status', DBCont::SUCCESS)
                ->column('team_id');
            if (empty($team)){
                $item['team_count'] = 0;
                $item['member_count'] = 0;
            } else {
                $item['team_count'] = model('Team')->whereIn('id', $team)->where('type', DBCont::TEAM)->count();
                $item['member_count'] = model('Team')->whereIn('id', $team)->where('type', DBCont::PERSON)->count();
            }

        }
    }
}